package com.it.plic;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.UUID;

/**
 * @author げっしょく
 * @date 2021/8/8
 */
public class ThreadD9 implements Runnable{
    private Socket accept;
    public ThreadD9(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bos = null;
        try {
            UUID uuid = UUID.randomUUID();
            bos = new BufferedOutputStream(new FileOutputStream("Day16\\" + uuid.toString().replace("-", "") + ".txt"));
            InputStream is = accept.getInputStream();
            int a;
            while ((a = is.read()) != -1){
                bos.write(a);
            }
            accept.shutdownInput();

            BufferedOutputStream bs = new BufferedOutputStream(accept.getOutputStream());
            bs.write("收到了，over".getBytes());
            bs.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept != null) {
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
